package com.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.logic.ConstantsValue;

public class ServerTransferDatabase 
{
	private DataBaseConnection dataBaseConnection=null;
	private Connection connection=null;
	private Statement statement=null;
	private ResultSet resultSet=null;
public ServerTransferDatabase() {
try
{
	dataBaseConnection = new DataBaseConnection();
	connection = dataBaseConnection.CallConnection();
	statement = connection.createStatement();
}
catch (Exception e) {
	e.printStackTrace();
}
}
public String OwnAccount(String[] req)
{
	boolean status=false;
	String result="";
	String accountNumber="";
	String amount="";
	String receiverBalance="";
	String senderNum="";
	String customerName="";
	accountNumber=req[1];
	amount=req[2];
	senderNum=req[3];
	try
	{
		System.out.println("########");
		System.out.println(req[1]);
		System.out.println(req[2]);
		System.out.println(req[3]);
		System.out.println("##########");
		resultSet=statement.executeQuery("select BALANCE,CUSTOMERNAME from ACCOUNT where accountnumber='"+accountNumber+"'");
		while(resultSet.next())
		{
			receiverBalance=resultSet.getString("BALANCE");
			customerName=resultSet.getString("CUSTOMERNAME");
			status=true;
		}
		if(!status)
		{
			result="Account Number Doesn't Exist";
		}
		if(status)
		{
			int bal=0,balDb=0;
			String balanceDb="";
			resultSet=statement.executeQuery("select * from ACCOUNT where accountnumber='"+senderNum+"'");
			while(resultSet.next())
			{
				balanceDb=resultSet.getString("BALANCE");
			}
			bal=Integer.parseInt(amount);
			balDb=Integer.parseInt(balanceDb);
			if(balDb>ConstantsValue.minimunBal&&(balDb-ConstantsValue.minimunBal)>bal)
			{
				balDb=balDb-bal;
				int plus=Integer.parseInt(receiverBalance)+bal;
				statement.executeUpdate("update ACCOUNT set BALANCE='"+String.valueOf(balDb)+"'  where ACCOUNTNUMBER='"+senderNum+"'" );
				statement.executeUpdate("update ACCOUNT set BALANCE='"+String.valueOf(plus)+"'  where ACCOUNTNUMBER='"+accountNumber+"'" );
				result="Amount Transferred to "+customerName;
			}
			else
			{
				result="Too low Balance to Transfer";
			}
		}
	}catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}
public String Search(String[] req)
{
	String actNum="";
	String result="";
	String chequeFrom="";
	String chequeTo="";
	actNum=req[1];
	chequeFrom=req[2];
	chequeTo=req[3];
	int currentCheck=0;
	String issue="";
	String deposit="";
	boolean status=false;
	boolean chequeStatus=false;
	try
	{
		resultSet=statement.executeQuery("select * from ACCOUNT where accountnumber='"+actNum+"'");
		while(resultSet.next())
		{
			status=true;
			currentCheck=Integer.parseInt(resultSet.getString("CHEQUENUM"));
			if(Integer.parseInt(chequeFrom)<=currentCheck || Integer.parseInt(chequeTo)>=currentCheck)
			{
				chequeStatus=true;
				issue=resultSet.getString("CHEQUESTATUS");
				if(issue!=null&&issue.equalsIgnoreCase("deposited"))
						{
					result="Deposited Amt is "+resultSet.getString("DEPOSITEDCHEQUEAMT");
						}
				else
				{
					result="Issued Amt is "+resultSet.getString("ISSUEDCHEQUEAMT");
					
				}
			}
		}	
		if(!status)
		{
			result="Account Number Doesn't Exist";
			
		}
		else if(status&&!chequeStatus)
		{
			result="Cheque Number Couldnt Find";
		}
		
	}
	catch (Exception e) {
		e.printStackTrace();
	}
	
	return result;
}
}
